package com.mall.shopping.services;

import com.mall.shopping.IProductCateService;
import com.mall.shopping.converter.ProductCateConverter;
import com.mall.shopping.dal.entitys.ItemCat;
import com.mall.shopping.dal.persistence.ItemCatMapper;
import com.mall.shopping.dto.AllProductCateRequest;
import com.mall.shopping.dto.AllProductCateResponse;
import com.mall.shopping.dto.ProductCateDto;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class IProductCateServiceImpl implements IProductCateService {

    @Autowired
    ItemCatMapper mapper;

    @Autowired
    ProductCateConverter converter;

    @Override
    public AllProductCateResponse getAllProductCate(AllProductCateRequest request) {
        Example example = new Example(ItemCat.class);
        example.createCriteria().andEqualTo("status", 1);
        List<ItemCat> itemCats = mapper.selectByExample(example);

        // 手机和服务去掉
        itemCats = itemCats.stream().filter(x -> !x.getName().trim().equals("手机"))
                                    .filter(x -> !x.getName().trim().equals("服务"))
                                    .collect(Collectors.toList());

        List<ProductCateDto> result = converter.items2Dto(itemCats);
        AllProductCateResponse response = new AllProductCateResponse();
        response.setProductCateDtoList(result);
        return response;
    }
}
